﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using PluginInterface;
using Buccaneer;
using System.Diagnostics;
using Buccontrols;

namespace Buccailor
{
    public delegate bool SafeCrossThreadCallNavigator(string text);

    public partial class FileBrowser : UserControl, IPlugin
    {
        #region Contructor

        public FileBrowser()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Attributes

        private IconButton[] lvButtons;

        #endregion

        #region Public Attributes

        public event EventHandler Navigating;

        #endregion

        #region Public Functions



        #endregion

        #region IPlugin Members

        static IPluginHost myPluginHost;
        PluginFunctions menuActions;
        string myPluginName = "FileBrowser";
        string myPluginAuthor = "Adriaan Dekker";
        string myPluginDescription = "The basic plugin used in Buccaneer";
        string myPluginVersion = "1.0.3";


        public virtual bool Navigate(string text)
        {
            if (this.InvokeRequired)
            {
                SafeCrossThreadCallNavigator d = new SafeCrossThreadCallNavigator(Navigate);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                try { Navigating.Invoke(this, EventArgs.Empty); }
                catch (NullReferenceException) { }
                lvButtons = GetFiles(text, new Size(123, 103), this.Width, 10);
                if (lvButtons == null)
                {
                    return false;
                }
                this.Controls.Clear();
                this.Controls.AddRange(lvButtons);
                lvButtons = null;
                this.Refresh();
                this.Update();
                this.PerformLayout();
            }
            return true;
        }

        public virtual void NavigateDown()
        {
            Host.Feedback("(i) Not implemented!", this);
        }

        public virtual void NavigateUp()
        {
            Host.Feedback("(i) Not implemented!", this);
        }

        public virtual void NavigateBack()
        {
            Host.Feedback("(i) Not implemented!", this);
        }

        public virtual void NavigateForward()
        {
            Host.Feedback("(i) Not implemented!", this);
        }

        public string Path
        {
            get { return currentPath; }
            set { Navigate(value); }
        }

        public string Description
        {
            get
            {
                // TODO:  Add ctlMain.Description getter implementation
                return myPluginDescription;
            }
        }

        public string Author
        {
            get
            {
                // TODO:  Add ctlMain.Author getter implementation
                return myPluginAuthor;
            }
        }

        public IPluginHost Host
        {
            get
            {
                // TODO:  Add ctlMain.Host getter implementation
                return myPluginHost;
            }
            set
            {
                myPluginHost = value;
            }
        }

        public void Initialize()
        {
            // TODO:  Add ctlMain.Initialize implementation
        }

        public PluginForm MainInterface
        {
            get
            {
                if (this.Parent == null)
                {
                    MainForm mf = new MainForm();
                    this.Parent = mf;
                }
                // TODO:  Add ctlMain.MainInterface getter implementation
                return this.Parent as PluginForm;
            }
        }

        public Daddy MainInterfaceParent
        {
            get
            {
                return this.MainInterface.MdiParent as Daddy;
            }
            set
            {
                this.MainInterface.MdiParent = value;
            }
        }

        public PluginFunctions MenuActions
        {
            get
            {
                return this.menuActions;
            }
            set
            {
                this.menuActions = value;
            }
        }

        public string Version
        {
            get
            {
                // TODO:  Add ctlMain.Version getter implementation
                return myPluginVersion;
            }
        }

        public string Name
        {
            get { return myPluginName; }
            set { return; }
        }

        public List<ActionFunction> ActionFunctions
        {
            get { return this.actionFuncs; }
        }

        #endregion

        private void FileBrowser_Load(object sender, EventArgs e)
        {
            Options.Font = (new FontDialog()).Font;
            IconButton.LoadImages();

            this.actionFuncs = new List<ActionFunction>();
            this.actionFuncs.Add(new ActionFunction(this.NavigateAction));
        }

        #region ActionFunctions

        public void NavigateAction(string[] args)
        {

        }

        #endregion
    }
}